/*
 * 机器输出测试
 * 舞者无罪
 * 2014.12.02
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "rock_tran.h"
#include "keypane.h"
#include <mh/common.h>

#define OUTPUT_TEST_CAPTION_WIDTH   (140)
#define OUTPUT_TEST_CAPTION_HEIGHT  (24)
#define OUTPUT_TEST_CAPTION_INTER   (6)
#define OUTPUT_TEST_COL_WIDTH       (90) //total width / 3
#define OUTPUT_TEST_CAPTION_LEFT    (CX_EDGE_LEFT * 2)
#define OUTPUT_TEST_CAPTION_TOP     (CY_TITLE4_HEIGHT + CX_EDGE_LEFT - 5)
#define OUTPUT_TEST_NAME_WIDTH      (60)


/*
 *
 */
static void CreateOutputTestInterface(HWND hWnd)
{
    CreateWindow (MH_LABEL_NAME,
        MW_LIGHT_STATE_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_DRIVER_ENABLE_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER),
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_MAC_DRT_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 2,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_MAC_SPEED_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 3,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_RAB_SPEED_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 4,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_RAB_OPEN_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 5,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_SPEED_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 6,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_ROLL_OPEN_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 7,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_LEFT_FEEDER_OUT_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 8,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    CreateWindow (MH_LABEL_NAME,
        MW_RGHT_FEEDER_OUT_CAPTION,
        WS_CHILD | BS_LEFT | WS_VISIBLE,
        IDC_STATIC,
        OUTPUT_TEST_CAPTION_LEFT,
        OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * 9,
        OUTPUT_TEST_CAPTION_WIDTH,
        OUTPUT_TEST_CAPTION_HEIGHT,
        hWnd, MH_LABEL_COLOR_1GROUP);
    int j;
    const char *scc[10] = {
        MW_CLOSE_CAPTION,
        MW_CLOSE_CAPTION,
        "<<<<<<",
        "0",
        "0",
        "0",
        "0",
        "0",
        MW_CLOSE_CAPTION,
        MW_CLOSE_CAPTION,
    };
    for(j = 0; j < 10; ++j)
    {
        CreateWindow (MH_EDIT_NAME,
            scc[j],
            WS_CHILD | WS_VISIBLE,
            IDC_EDIT1 + j,
            OUTPUT_TEST_CAPTION_LEFT + OUTPUT_TEST_CAPTION_WIDTH,
            OUTPUT_TEST_CAPTION_TOP + (OUTPUT_TEST_CAPTION_HEIGHT + OUTPUT_TEST_CAPTION_INTER) * j,
            OUTPUT_TEST_CAPTION_WIDTH,
            OUTPUT_TEST_CAPTION_HEIGHT,
            hWnd, 0);
    }
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        CX_YARN_CHANGE_BMP - CX_ASK_BMP * 2,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
}
/*
 *
 */
static void OnSetKnitValue(HWND hWnd, int nID)
{
    const char *sc = GetWindowCaption(GetDlgItem(hWnd, nID));
    switch(nID)
    {
    case IDC_EDIT1 + 4:
        {
            char n = atoi(sc);
            RunRab(g_knit_data, n, FALSE);
        }
        break;
    case IDC_EDIT1 + 5:
        {
            uchar n = atoi(sc);
            RunRab(g_knit_data, -n, FALSE);
        }
        break;
    case IDC_EDIT1 + 6:
        {
            uchar n = atoi(sc);
            RunRoller(g_knit_data, n);
        }
        break;
    case IDC_EDIT1 + 7:
        {
            uchar n = atoi(sc);
            RunRoller(g_knit_data, -n);
        }
        break;
    }
}
/*
 *
 */
static void OnCreateKeyboard(HWND hWnd, ushort nID)
{
    int index = 0;
    switch(nID)
    {
    case IDC_EDIT1 + 3:
        index = 0;
        break;
    case IDC_EDIT1 + 4:
        index = 1;
        break;
    case IDC_EDIT1 + 5:
        index = 2;
        break;
    case IDC_EDIT1 + 6:
        index = 3;
        break;
    case IDC_EDIT1 + 7:
        index = 4;
        break;
    }
    int area[5][2] = { {0, 30 }, {0, 100 }, {0, 500 }, {0, 100 }, {0, 100 }};
    HWND hTmp = GetDlgItem(hWnd, nID);
    RECT rect;
    GetClientRect(hTmp, &rect);
    ClientToScreen(hTmp, &rect.left, &rect.top);

    ClientToScreen(hTmp, &rect.right, &rect.bottom);
    int value = atoi(GetWindowCaption(hTmp));
    CreateKeyboardWindow(hWnd, rect, area[index][0], area[index][1], value);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case IDC_EDIT1:
        {
            uchar n = GetSignalLightState(g_knit_data);
            const char *sc = MW_CLOSE_CAPTION;
            switch(n)
            {
            case SIGNAL_LIGHT_STATE_OFF:
                sc = MW_RED_LIGHT_CAPTION;
                n =  SIGNAL_LIGHT_STATE_RED;
                break;
            case SIGNAL_LIGHT_STATE_RED:
                sc = MW_GREEN_LIGHT_CAPTION;
                n = SIGNAL_LIGHT_STATE_GREEN;
                break;
            case SIGNAL_LIGHT_STATE_GREEN:
                sc= MW_YELLOW_LIGHT_CAPTION;
                n = SIGNAL_LIGHT_STATE_YELLOW;
                break;
            case SIGNAL_LIGHT_STATE_YELLOW:
                sc = MW_CLOSE_CAPTION;
                n = SIGNAL_LIGHT_STATE_OFF;
                break;
            }
            SetWindowCaption(GetDlgItem(hWnd, nID), sc);
            InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
            ToggleSignalLight(g_knit_data);
        }
        break;
    case IDC_EDIT1 + 1:
        {
            uchar n = GetMacDriverEnable(g_knit_data);
            SetWindowCaption(GetDlgItem(hWnd, nID), n ? MW_CLOSE_CAPTION : MW_OPEN_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
            ToggleMacDriverEnable(g_knit_data);
            if(!n)
            {
                OnSetKnitValue(hWnd, nID);
            }
            else
            {
                RunMotor(g_knit_data, 0);
                RunRoller(g_knit_data, 0);
                RunRab(g_knit_data, 0, 0);
            }
        }
        break;
    case IDC_EDIT1 + 2:
        {
            WND_EXTRA_DATA data;
            uchar n = GetMacDriverEnable(g_knit_data);
            data.value = GetWindowAdditionalData(hWnd);
            const char *p =  GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 2));
            if(strcmp(p, ">>>>>>") == 0)
            {
                const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 3));
                n = atoi(sc);
                data.output.left = TRUE;
                data.output.right = FALSE;
                RunMotor(g_knit_data, -n);
                SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 2), "<<<<<<");
                InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 2), NULL, TRUE);
            }
            else
            {
                const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 3));
                n = atoi(sc);
                data.output.right = TRUE;
                data.output.left = FALSE;
                RunMotor(g_knit_data, n);
                SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 2), ">>>>>>");
                InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 2), NULL, TRUE);
            }
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 1),  MW_OPEN_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 1), NULL, TRUE);
            ToggleMacDriverEnable(g_knit_data);
            SetWindowAdditionalData(hWnd, data.value);
        }
        break;
    case IDC_EDIT1 + 3:
    case IDC_EDIT1 + 4:
    case IDC_EDIT1 + 5:
    case IDC_EDIT1 + 6:
    case IDC_EDIT1 + 7:
        {
            OnCreateKeyboard(hWnd, nID);
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 1),  MW_OPEN_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 1), NULL, TRUE);
            ToggleMacDriverEnable(g_knit_data);
        }
        break;
    case IDC_EDIT1 + 8://左送纱
        {
            uchar n = GetLeftYarnFeederState(g_knit_data);
            SetWindowCaption(GetDlgItem(hWnd, nID), n ? MW_CLOSE_CAPTION : MW_OPEN_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
            ToggleLeftYarnFeeder(g_knit_data);
        }
        break;
    case IDC_EDIT1 + 9://右送纱
        {
            uchar n = GetRightYarnFeederState(g_knit_data);
            SetWindowCaption(GetDlgItem(hWnd, nID), n ? MW_CLOSE_CAPTION : MW_OPEN_CAPTION);
            InvalidateRect(GetDlgItem(hWnd, nID), NULL, TRUE);
            ToggleRightYarnFeeder(g_knit_data);
        }
        break;
    case ID_ESC:
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
        break;
    default:
        {
            HWND hTmp = GetDlgItem(hWnd, nID);
            RECT rect;
            GetClientRect(hTmp, &rect);
            ClientToScreen(hTmp, &rect.left, &rect.top);
            ClientToScreen(hTmp, &rect.right, &rect.bottom);
            int value = atoi(GetWindowCaption(hTmp));
            CreateKeyboardWindow(hWnd, rect, 0, 100, value);
        }
        break;
    }
}

/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_ENTER)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        OnBtnCmd(hWnd, nID);
    }
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT1 + 2)
        {
            uchar n = 0;
            if(data.output.left)
            {
                data.output.left = FALSE;
            }
            else
            {
                const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 3));
                n = atoi(sc);
                data.output.left = TRUE;
                data.output.right = FALSE;
            }
            RunMotor(g_knit_data, -n);
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 2), "<<<<<<");
            InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 2), NULL, TRUE);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID == IDC_EDIT1 + 2)
        {
            uchar n = 0;
            if(data.output.right)
            {
                data.output.right = FALSE;
            }
            else
            {
                const char *sc = GetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 3));
                n = atoi(sc);
                data.output.right = TRUE;
                data.output.left = FALSE;
            }
            RunMotor(g_knit_data, n);
            SetWindowCaption(GetDlgItem(hWnd, IDC_EDIT1 + 2), ">>>>>>");
            InvalidateRect(GetDlgItem(hWnd, IDC_EDIT1 + 2), NULL, TRUE);
        }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= IDC_EDIT1 && nID < IDC_EDIT1 + 9)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > IDC_EDIT1 && nID <= IDC_EDIT1 + 9)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    SetWindowAdditionalData(hWnd, data.value);
}
/*
 *
 */
static int OutputTestWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateOutputTestInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 30;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 30;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup (hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls (hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_YARN_CHANGE]);
        }
        return 0;
    case MSG_SETDATA:
        OnSetKnitValue(hWnd, LOWORD(wParam));
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateOutputTestWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int x = (CX_SCREEN - CX_YARN_CHANGE_BMP) / 2;
    int y = (CY_SCREEN - CY_YARN_CHANGE_BMP) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_OUTPUT_TEST_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = OutputTestWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + CX_YARN_CHANGE_BMP;
    CreateInfo.by = y + CY_YARN_CHANGE_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SwitchToKnitState(g_knit_data, FALSE);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, IDC_EDIT1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    else
    {
    }
    return hWnd;
}
